package yannan.voiplock4ble.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import yannan.voiplock4ble.R;

/**
 * <p>name:</p>
 * <p>describe:倒计时按钮</p>
 *
 * @author Maintel
 * @time 2016/9/25 20:42
 */
public class TimeButton extends Button implements View.OnClickListener {

    long length = 30 * 1000;// 倒计时长度,这里给了默认60秒
    String textafter = "秒后重新获取";
    String textbefore = "重新获取验证码";

    Timer t;
    TimerTask tt;
    long time;
    Context mContext;
    boolean isStop = false;
    Map<String, Long> map = new HashMap<String, Long>();


    public TimeButton(Context context) {
        super(context);
        setOnClickListener(this);
    }

    public TimeButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setOnClickListener(this);
    }

    public TimeButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @SuppressLint("HandlerLeak")
    Handler han = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (!isStop) {
                TimeButton.this.setText(time / 1000 + textafter);
                time -= 1000;
                if (time < 0) {
                    clearTimer();
                }
            } else {
                clearTimer();
            }

        }
    };

    private void initTimer() {
        time = length;
        t = new Timer();
        tt = new TimerTask() {
            @Override
            public void run() {
                if (!isStop) {
                    han.sendEmptyMessage(0x01);
                }
            }
        };
    }

    private void clearTimer() {
        if (tt != null) {
            tt.cancel();
            tt = null;
        }
        if (t != null)
            t.cancel();
        t = null;
        TimeButton.this.setEnabled(true);
        TimeButton.this.setText(textbefore);
        TimeButton.this.setBackgroundResource(R.drawable.button_login_bg);
    }


    @Override
    public void onClick(View v) {
        initTimer();
        isStop = false;
        this.setText(time / 1000 + textafter);
        TimeButton.this.setBackgroundResource(R.drawable.btn_time_off_bg);
        this.setEnabled(false);
        t.schedule(tt, 0, 1000);
        if (mOnclickListener != null)
            mOnclickListener.onClick();
    }

    public void setMOnClickListener(IOnClickListener e) {
        mOnclickListener = e;
    }

    IOnClickListener mOnclickListener;

    public interface IOnClickListener {
        void onClick();
    }

    /**
     * 设置计时时候显示的文本
     */
    public TimeButton setTextAfter(String text1) {
        this.textafter = text1;
        return this;
    }

    public void stopTime() {
        isStop = true;
        clearTimer();
    }

    /**
     * 设置点击之前的文本
     */
    public TimeButton setTextBefore(String text0) {
        this.textbefore = text0;
        this.setText(textbefore);
        return this;
    }

    /**
     * 设置到计时长度
     *
     * @param lenght 时间 默认毫秒
     * @return
     */
    public TimeButton setLenght(long lenght) {
        this.length = lenght;
        return this;
    }

}
